//
//  meTableViewController.m
//  企业信息查询
//
//  Created by 梁中豪 on 16/4/7.
//  Copyright © 2016年 梁中豪. All rights reserved.
//

#import "meTableViewController.h"
#import "dengluViewController.h"
#import <BmobSDK/Bmob.h>
@interface meTableViewController ()

- (IBAction)cancelDL:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *login;
@property (weak, nonatomic) IBOutlet UIButton *infome;
- (IBAction)infome2:(id)sender;

@end

@implementation meTableViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.login setBackgroundImage:[UIImage imageNamed:@"head1.png"] forState:UIControlStateNormal];
    
  //通知模式  为了传递 登录界面的用户名 因为有了更好的方法 所以注释掉这段
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeNameNotification:) name:@"ChangeNameNotification" object:nil];
    
    BmobUser *tempusr = [BmobUser getCurrentUser];
    if (tempusr) {
        self.login.userInteractionEnabled = NO;
        [self.login setBackgroundImage:[UIImage imageNamed:@"head2.png"] forState:UIControlStateNormal];
        _usrDL.text =[tempusr objectForKey:@"username"];
    }
     else{
         //self.infome.userInteractionEnabled = NO;
     }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

    //通知中心 调用方法
- (void)ChangeNameNotification:(NSNotification *)notification {
   
    NSDictionary *nameDictionary = [notification userInfo];
    self.usrDL.text = [nameDictionary objectForKey:@"name"];
    BmobUser *tempusr = [BmobUser getCurrentUser];
    if (tempusr) {
        self.login.userInteractionEnabled = NO;
        self.infome.userInteractionEnabled = YES;
        _usrDL.text =[tempusr objectForKey:@"username"];
        [self.login setBackgroundImage:[UIImage imageNamed:@"head2.png"] forState:UIControlStateNormal];
    }
    
  }



- (IBAction)cancelDL:(id)sender {
   UIAlertController *zhanghaocancel = [UIAlertController
                                        alertControllerWithTitle:@"退出"
                                        message:@"确定退出？"
                                        preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定"
                                                        style:UIAlertActionStyleCancel
                                                      handler:^(UIAlertAction *action){
                                                          _usrDL.text = @"请登录";
                                                          [BmobUser logout];
                                                          [self.login setBackgroundImage:[UIImage imageNamed:@"head1.png"] forState:UIControlStateNormal];
                                                          self.login.userInteractionEnabled =YES;
                                                      }];
    UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"取消"
                                                        style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction *action){
                                                      }];

    [zhanghaocancel addAction:yesAction];
    [zhanghaocancel addAction:noAction];
    
    //显示
    [self presentViewController:zhanghaocancel animated:true completion:nil];
    
    
}

- (IBAction)infome2:(id)sender {
    BmobUser *tempusr2 = [BmobUser getCurrentUser];
    if(!tempusr2){
         self.infome.userInteractionEnabled = NO;
         UIAlertController *meinfo = [UIAlertController
                                 alertControllerWithTitle:@"错误"
                                 message:@"请先登录"
                                 preferredStyle:UIAlertControllerStyleAlert];
    
         UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定"
                                                        style:UIAlertActionStyleCancel
                                                      handler:^(UIAlertAction *action){
                                                          
                                                      }];
    [meinfo addAction:yesAction];
    [self presentViewController:meinfo animated:true completion:nil];
    }
}
@end
